package model.data.container;

/**
 * OrderContainer.java
 * Purpose: A container to hold order objects.
 * 
 * @author Hans Brems
 * @version 0.1 12/01/2012
 */

import java.util.ArrayList;
import java.util.List;

import model.Order;
import model.data.DuplicateRecordException;
import model.data.OrderStore;
import model.data.RecordNotFoundException;

public class OrderContainer implements OrderStore {
	/* List of orders */
	private ArrayList<Order> orders;
	
	/** Constructs a new OrderContainer */
	public OrderContainer() {
		this.orders = new ArrayList<Order>();
	}
	
	/** Writes a new order */
	@Override
	public void write(Order order) throws DuplicateRecordException {
		if (this.getById(order.getId()) == null) {
			this.orders.add(order);
		}
		else
		{
			throw new DuplicateRecordException();
		}
	}

	/** Updates an existing order */
	@Override
	public void update(Order order) {
		// TODO update logic
		// currently impossible due to referencing issue
	}

	/** Deletes an existing order */
	@Override
	public void delete(Order order) throws RecordNotFoundException {
		if (this.getById(order.getId()) != null) {
			this.orders.remove(order);
		}
		else
		{
			throw new RecordNotFoundException();
		}
	}

	/** Returns the number of orders in the store */
	@Override
	public int getCount() {
		return this.orders.size();
	}

	/** Returns an order from the container with a specific id */
	@Override
	public List<Order> getById(long id) {
		ArrayList<Order> orders = new ArrayList<Order>();
		for (Order order : this.orders) {
			if (order.getId() == id) {
				orders.add(order);
			}
		}
		return orders;
	}
}